import time

from selenium.webdriver.common.by import By

from jiuxian111.Page.Page import Page
class Page1(Page):
    # # 输入框输入茅台
    # search_loc=(By.XPATH,'//*[@id="wd"]')
    # # 点击搜索
    # searchbtn_loc=(By.CLASS_NAME,'search-btn')
    # # 点击商品进入商品详情
    # goods_loc=(By.XPATH,'/html/body/div[6]/div/div[2]/div[5]/ul/li[1]/div[2]/div[1]/a/img')
    # # 点击加入购物车
    # add_loc=(By.LINK_TEXT,'加入购物车')
    # # 查看购物车addToCartForDetail
    # lookadd_loc=(By.XPATH,'//*[@id="u-buy-layId"]/div/div[3]/a[2]')
    # # 点击结算
    # js_loc=(By.CLASS_NAME,'done')
    # suit_loc=(By.ID,'submitOrder')
    #
    # def search(self):
    #     self.send_keys(self.search_loc,'茅台')
    # def searchbtn(self):
    #     self.click(self.searchbtn_loc)
    # def goods(self):
    #     self.click(self.goods_loc)
    # def add(self):
    #     self.click(self.add_loc)
    # def lookadd(self):
    #     self.click(self.lookadd_loc)
    # def js(self):
    #     self.click(self.js_loc)
    # def suit(self):
    #     self.click(self.suit_loc)
    # def jiuxian_luoji1(self):
    #     self.jiuxian_luoji()
    #     self.search()
    #     self.searchbtn()
    #     self.goods()
    #     self.windows()
    #     time.sleep(2)
    #     self.add()
    #     self.windows()
    #     self.lookadd()
    #     self.windows()
    #     self.js()
    #     self.windows()
    #     self.scroll_down()
    #     self.suit()
    #     time.sleep(2)
    # 点击请登录 class_name
    login_loc = (By.CLASS_NAME, 'headerLogin')
    # 断言登录页面
    login_text_loc = (By.XPATH, '//*[@id="loginform"]/div/div/div/div/div[1]/div[1]/a[1]')
    # 输入账号 id
    user_loc = (By.ID, 'userName')
    # 输入密码 name
    pwd_loc = (By.NAME, '_pwd')
    # 强制等待10秒 输入文本验证码
    # 点击登录 css_selector id属性
    login_btn_loc = (By.CSS_SELECTOR, '#save')
    # 断言登录成功
    login_success_loc = (By.XPATH, '/html/body/div[2]/div/div[1]/div/p/span')
    # 点击确定 css_selector class_name属性
    submit_loc = (By.CSS_SELECTOR, '.bindSub')
    # 搜索框
    search_loc = (By.ID, 'wd')
    # 搜索
    searchbtn_loc=(By.CLASS_NAME,'search-btn')
    # 商品详情 xpath
    good_loc = (By.XPATH, '/html/body/div[6]/div/div[2]/div[5]/ul/li[2]/div[2]/div[3]/a')
    # 断言商品详情
    good_info_text_loc = (By.XPATH, '/html/body/div[5]/div[2]/div[2]/div[1]/h1')
    # 加入购物车 xpath and
    add_car_loc = (By.XPATH, '//a[@name="detail_jiarugouwuche" and @id="addToCartForDetail"]')
    # 查看购物车 xpath or
    look_car_loc = (By.XPATH, '//a[@name="detail_chakangouwuche" or @class="u-buy-go"]')
    # 断言我的购物车
    me_car_loc = (By.XPATH, '//*[@id="cart_container"]/div[2]/div[1]/div[1]')
    # 点击去结算 绝对定位
    buy_loc = (By.XPATH, '/html/body/div[3]/div[1]/div[3]/div[3]/a[1]')
    # 点击提交订单
    create_loc = (By.ID, 'submitOrder')
    # 断言购买成功
    create_success = (By.XPATH, '/html/body/div[4]/div/div[1]/ul/li[1]')

    # 封装页面元素
    # 点击请登录
    def login(self):
        self.click(self.login_loc)

    def logon_text(self):
        return self.text(self.login_text_loc)

    # 输入账号
    def username(self):
        self.send_keys(self.user_loc, '15253006116')

    # 输入密码
    def password(self):
        self.send_keys(self.pwd_loc, 'Aa123456')

    # 点击登录
    def login_btn(self):
        self.click(self.login_btn_loc)

    def login_success(self):
        return self.text(self.login_success_loc)

    # 点击确定
    def submit(self):
        self.click(self.submit_loc)

    # 搜索框输入茅台
    def search(self):
        self.send_keys(self.search_loc, '茅台')

    def searchbtn(self):
        self.click(self.searchbtn_loc)

    # 商品详情
    def good_info(self):
        self.click(self.good_loc)

    def good_info_text(self):
        return self.text(self.good_info_text_loc)

    # 点击加入购物车
    def add_car(self):
        self.click(self.add_car_loc)

    # 点击查看购物车
    def look_car(self):
        self.click(self.look_car_loc)

    def me_car(self):
        return self.text(self.me_car_loc)

    # 点击去结算
    def buy(self):
        self.click(self.buy_loc)

    # 点击提交订单
    def create(self):
        self.click(self.create_loc)

    def create_sucess(self):
        return self.text(self.create_success)

    # 业务逻辑
    def jiuxian_luoji2(self):
        self.open_url('https://www.jiuxian.com/')
        # 刷新页面
        # self.refresh()
        self.login()
        self.logon_text()
        print(self.logon_text())
        assert '帐号登录' == self.logon_text()
        self.username()
        self.password()
        time.sleep(10)
        self.login_btn()
        self.submit()
        self.login_success()
        print(self.login_success())
        assert 'jxw386334645' == self.login_success()
        self.search()
        self.searchbtn()
        self.good_info()
        self.windows()
        self.good_info_text()
        print(self.good_info_text())
        assert '茅台' in self.good_info_text()
        self.add_car()
        self.look_car()
        self.me_car()
        print(self.me_car())
        assert '我的购物车' in self.me_car()
        self.buy()
        self.scroll_x(800)
        self.create()
        self.create_sucess()
        print(self.create_sucess())
        assert '提交成功' in self.create_sucess()



